/**
 * 列表
 */
user.userGrid = user.userGrid || Ext.extend(Ext.grid.GridPanel,{
	 initComponent:function(){
	 	var sm = new Ext.grid.CheckboxSelectionModel({singleSelect:true});
	 	var cm = new Ext.grid.ColumnModel([
        		 sm,new Ext.grid.RowNumberer(),
		         {header:'用户Id',dataIndex:"userId", hidden:true},
		         {header:'账号',dataIndex:"account",sortable:true},
		         {header:'用户名',dataIndex:"realName",sortable:true},
		         {header:'机构',dataIndex:"organName",sortable:true},
		         {header:'密码',dataIndex:"password",hidden:true},
		         {header:'性别',dataIndex:"sex",sortable:true,
		         	renderer:function(v){
			         	if(v==0){return '男';}else{return '女';}
		         	}},
		         {header:'email',dataIndex:"email",sortable:true},
		         {header:'电话',dataIndex:"mobile",sortable:true},
		         {header:'备注',dataIndex:"remark",sortable:true}
			         
	    ]);
	    cm.defaultSortable = true;
	    var fields = [
				 {name:"userId"},
				 {name:"account"},
				 {name:"realName"},
				 {name:"organId"},
				 {name:"password"},
				 {name:"sex"},
				 {name:"email"},
				 {name:"mobile"},
				 {name:"organName"},
				 {name:"remark"}
		];
		var ds = new Ext.data.Store({
				proxy:new Ext.data.HttpProxy({
		        	method: 'POST',
		        	url:'user/getDataList.jhtml'
		        }),
		        reader:new Ext.data.JsonReader({
		            root:'rows',
		            totalProperty:'results',
		            fields: fields
		        }),
		        baseParams:{
		    	    start:0,
		            limit: 20
		        },
		        autoLoad:true
		});
	   Ext.apply(this, {
		 	store:ds,
            sm:sm,
            cm:cm,
            title:'用户列表',
            border:false,
            region : 'center',
    		stripeRows:true,
            loadMask: {msg:'正在加载数据，请稍候....'},
			layout:'fit',
            bbar:new Ext.PagingToolbar({
                pageSize: 20,
                store:ds,
                displayInfo:true
            }),
            tbar:[],
            formWindow: new user.win(),
            roleWindow: new user.roleWin()
		 });
	 user.userGrid.superclass.initComponent.call(this);
	 var builder = new ButtonBuilder(this,user.moduleId,'grid');
 	 builder.build(this.getTopToolbar());
	},
	//add
	addFn:function(){
		var addWin = this.ownerCt.ownerCt.formWindow;
		if(!addWin){
			addWin = new user.win();
		}
		addWin.setIconClass('add');															
		addWin.setTitle('添加用户');																	
		addWin.show();	
		addWin.findByType('form')[0].form.reset();
	},
	//edit
	editFn:function(){
		var grid = this.ownerCt.ownerCt;
		var record = grid.getSelectionModel().getSelected();			
		if (record == null) {Ext.Msg.alert('提示','请选择一条记录！');return;}	
		var editWin = grid.formWindow;
		if(!editWin){
			editWin = new user.win();
		}
	  	editWin.setIconClass('edit');														
	  	editWin.setTitle('编辑用户');																
	  	editWin.show();
	  	var win_form = editWin.findByType('form')[0];
	  	win_form.form.reset();												
	  	win_form.form.loadRecord(record);
	},
	//del
	delFn:function(button,v){
		var grid = this.ownerCt.ownerCt;
		var record = grid.getSelectionModel().getSelected();			
		if (record == null) {Ext.Msg.alert('提示','请选择一条记录！');return;}											
		Ext.Msg.confirm('提示', '确定要删除这条记录吗?', function (btn, text) {											
			if (btn == 'yes') {																					
			 var waiting = Ext.Msg.wait('正在处理，请稍等...', '', '');												
				Ext.Ajax.request({																				
					url:'user/delUserByUserId.jhtml',																		
					params:{userId:record.data.userId},																	
					method:'POST',																				
					success:function(response, options){														
						if(waiting!=null){waiting.hide();}														
						var json = Ext.decode(response.responseText);											
						if(json.success==true){																	
							Ext.Msg.alert('提示',json.msg,function(){												
								grid.getStore().reload();											
							});																					
						}else{																					
							Ext.Msg.alert('提示','发生异常！');														
						}																						
					},																							
					failure:function(response, options){														
						if(waiting!=null){waiting.hide();}														
						var json = Ext.decode(response.responseText);											
						Ext.Msg.alert('提示',json.msg==''?'发生异常！':json.msg);															
					}																							
				});																								
			}																									
		});		
	},
	//所属角色
	roleFn:function(){
		var grid = this.ownerCt.ownerCt;
		var record = grid.getSelectionModel().getSelected();			
		if (record == null) {Ext.Msg.alert('提示','请选择一条记录！');return;}	
		var roleWin = this.ownerCt.ownerCt.roleWindow;
		if(!roleWin){
			roleWin = new user.roleWin();
		}
		roleWin.setIconClass('role');															
		roleWin.setTitle('选择角色');																	
		roleWin.items.get(0).root.reload();
		roleWin.items.get(0).root.expand(true);
		roleWin.show();
	},
	//查询
	searchFn:function(){
		
	}
});